package com.yx.system.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.yx.common.annotation.Log;
import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.TdBanner;
import com.yx.system.entity.TdBook;
import com.yx.system.service.TdBannerService;
import com.yx.system.service.TdBookService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * BannerController
 * Created by wuye on 2017/12/18.
 */
@RestController
@RequestMapping("/sys/banner")
public class TdBannerConroller2 {

    @Autowired
    private TdBannerService tdBannerService;

    @Autowired
    private TdBookService tdBookService;

    /**
     * 所有用户列表
     */
    @PostMapping("/saveUpdate")
    @RequiresPermissions("sys:banner:saveUpdate")
    public R saveUpdate(TdBanner tdBanner, @LoginUser2 SysUser sysUser) {
        if (tdBanner.getId() == null || tdBanner.getId().equals("")) {
            tdBanner.setCreatetime(new Date());
            tdBanner.setCreateuser(sysUser.getUsername());
            tdBanner.setUpdatetime(new Date());
            tdBanner.setUpdateuser(sysUser.getUsername());
            tdBannerService.save(tdBanner);
        } else {
            tdBanner.setUpdatetime(new Date());
            tdBanner.setUpdateuser(sysUser.getUsername());
            tdBannerService.update(tdBanner);
        }
        return R.ok();
    }


    /**
     * 按ID查询bannaer
     *
     * @return
     */
    @RequestMapping("/findById/{id}")
    public R findById(@PathVariable("id") int id) {
        return R.ok().put("banner", tdBannerService.selectByPrimaryKey(id));
    }

    /**
     * 查询所有
     *
     * @param map
     * @return
     */
    @GetMapping("findAllByCondition")
    @RequiresPermissions("sys:banner:findAllByCondition")
    public R findAllByCondition(@RequestParam Map<String, Object> map) {
        Query query = new Query(map);
        Page<TdBanner> pageUtil = new Page<TdBanner>(query.getPage(), query.getLimit());
        Page<TdBanner> page = tdBannerService.queryPageList(pageUtil, query);
        return R.ok().put("page", page);
    }


    /**
     * 新增或修改
     *
     * @return
     */
    @Log("新增或修改Banner")
    @PostMapping("saveOrUpdate")
    public R saveOrUpdate(@RequestBody TdBanner tdBanner, @LoginUser2 SysUser sysUse) {
        System.out.println(tdBanner.getBookid());
        if (tdBanner.getId() == null) {
            tdBanner.setCreatetime(new Date());
            tdBanner.setCreateuser(sysUse.getUsername());
            tdBanner.setUpdatetime(new Date());
            tdBanner.setUpdateuser(sysUse.getUsername());
            TdBook tdBook = tdBookService.selectByPrimaryKey(tdBanner.getBookid());
            tdBanner.setBookname(tdBook.getBookname());
            tdBannerService.insert(tdBanner);
        } else {
            tdBanner.setUpdatetime(new Date());
            tdBanner.setUpdateuser(sysUse.getUsername());
            TdBook tdBook = tdBookService.selectByPrimaryKey(tdBanner.getBookid());
            tdBanner.setBookname(tdBook.getBookname());
            tdBannerService.updateByPrimaryKey(tdBanner);
        }
        return R.ok();
    }

    /**
     * 删除
     *
     * @return
     */
    @Log("删除banner")
    @PostMapping("delete")
    public R delete(@RequestBody Integer[] ids) {
        for (int i =0;i<ids.length;i++) {
            int id = ids[i];
            tdBannerService.deleteByPrimaryKey(id);
        }
        return R.ok();
    }
}
